package com.example.myapplication;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.ViewPager;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.myapplication.utils.GuidePageTool;
import com.example.myapplication.utils.IpandPort;

import java.util.ArrayList;
import java.util.List;

public class GuidePageActivity extends AppCompatActivity {
    private TextView network;
    private TextView homepage;
    private ViewPager viewPager;
    private SharedPreferences sharedPreferences;
    private SharedPreferences.Editor editor;
    private List<Integer> list;
    private List<View> listView;
    private View view1;
    private View view2;
    private View view3;
    private View view4;
    private View view5;
    private boolean isFirst;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guide_page);
        sharedPreferences  =getSharedPreferences("isFirstHome", Context.MODE_PRIVATE);
        editor = sharedPreferences.edit();
        isFirst = sharedPreferences.getBoolean("isFirst",true);
        if(isFirst){
            init();
            initGuide();
            initNetwork();
            homepage.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    editor.putBoolean("isFirst",false);
                    editor.apply();
                    enterHome();
                }
            });
        }else{
            enterHome();
        }

    }

    private void initNetwork() {
        network.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(GuidePageActivity.this);
                View window = LayoutInflater.from(GuidePageActivity.this).inflate(R.layout.guide_page_network,null);
                EditText ip = window.findViewById(R.id.ip);
                EditText port = window.findViewById(R.id.port);
                ip.setText(IpandPort.IP);
                port.setText(IpandPort.PORT);
                builder.setView(window);
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        String s = ip.getText().toString();
                        String s1 = port.getText().toString();
                        IpandPort.IP = s;
                        IpandPort.PORT = s1;
                        Toast.makeText(GuidePageActivity.this,"保存成功",Toast.LENGTH_SHORT).show();
                    }
                });
                AlertDialog alertDialog = builder.create();
                alertDialog.show();
                // 设置按钮的颜色
                Button positiveButton = alertDialog.getButton(DialogInterface.BUTTON_POSITIVE);
                Button negativeButton = alertDialog.getButton(DialogInterface.BUTTON_NEGATIVE);

                // 设置按钮的背景颜色为白色
                positiveButton.setBackgroundColor(Color.WHITE);
                negativeButton.setBackgroundColor(Color.WHITE);

                positiveButton.setTextColor(Color.BLACK); // 设置"确定"按钮的颜色
                negativeButton.setTextColor(Color.BLACK); // 设置"取消"按钮的颜色
            }
        });
    }


    private void enterHome() {
        Intent intent = new Intent(this,MainActivity.class);
        this.startActivity(intent);
    }

    private void initGuide() {
        list = new ArrayList<>();
        list.add(R.drawable.ydy);
        list.add(R.drawable.ydy2);
        list.add(R.drawable.ydy3);
        list.add(R.drawable.ydy4);
        list.add(R.drawable.ydy5);
        viewPager.setAdapter(new GuidePageTool(this,list,viewPager));
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                for (int i = 0; i < listView.size(); i++) {
                    if(position == i){
//                        yuan_list.get(i).setBackground(getDrawable(R.drawable.l_yuan_white));
                        listView.get(i).setBackground(getDrawable(R.drawable.ydy_red));
                    }else{
                        listView.get(i).setBackground(getDrawable(R.drawable.ydy_white));

                    }
                }
                if(list.size()-1 == position){
                    network.setVisibility(View.VISIBLE);
                    homepage.setVisibility(View.VISIBLE);
                }else{
                    network.setVisibility(View.GONE);
                    homepage.setVisibility(View.GONE);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void init() {
        network  = findViewById(R.id.network);
        homepage  = findViewById(R.id.homepage);
        network.setVisibility(View.GONE);
        homepage.setVisibility(View.GONE);
        viewPager  = findViewById(R.id.viewPager);

        view1 = findViewById(R.id.view1);
        view2 = findViewById(R.id.view2);
        view3 = findViewById(R.id.view3);
        view4 = findViewById(R.id.view4);
        view5 = findViewById(R.id.view5);
        listView = new ArrayList<>();
        listView.add(view1);
        listView.add(view2);
        listView.add(view3);
        listView.add(view4);
        listView.add(view5);

    }
}